var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    categoryStatus: false,
    multiArray: [],
    objectMultiArray: [],
    multiIndex: [],
    oneid: '',
    twoid: '',
    twoname: '',

    categoryIdOne: '',
    categoryIdTwo: '',
    listCategoryOne: [],

    num: '', //数量
    weight: '', //重量
    items: [
      {
        name: '1',
        value: '包',
        checked: true
      },
      {
        name: '2',
        value: '袋',
        checked: false
      }
    ],
    unitType: '1' //1包 、2 件
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.queryall();
  },
  //品类选择
  bindMultiPickerChange: function(e) {
    var that = this;
    that.setData({
      'multiIndex[0]': e.detail.value[0],
      'multiIndex[1]': e.detail.value[1],
      categoryStatus: true,
      categoryIdOne: that.data.objectMultiArray[e.detail.value[0]].parid,
      categoryIdTwo:
        that.data.objectMultiArray[e.detail.value[0]].array[e.detail.value[1]]
          .parid
    });
    that.setData({
      oneid: that.data.objectMultiArray[e.detail.value[0]].parid
    });
    that.setData({
      twoid:
        that.data.objectMultiArray[e.detail.value[0]].array[e.detail.value[1]]
          .parid
    });
    that.setData({
      twoname:
        that.data.objectMultiArray[e.detail.value[0]].array[e.detail.value[1]]
          .regname
    });
  },
  bindMultiPickerColumnChange: function(e) {
    var that = this;
    switch (e.detail.column) {
      case 0:
        var list = [];
        for (var i = 0; i < that.data.objectMultiArray.length; i++) {
          for (
            var j = 0;
            j < that.data.objectMultiArray[e.detail.value].array.length;
            j++
          ) {
            if (
              that.data.objectMultiArray[i].parid ==
              that.data.objectMultiArray[e.detail.value].array[j].regid
            ) {
              list.push(that.data.objectMultiArray[i].array[j].regname);
            }
          }
        }
        that.setData({
          'multiArray[1]': list,
          'multiIndex[0]': e.detail.value,
          'multiIndex[1]': 0
        });
    }
  },

  //是否结算
  radioChange: function(e) {
    var that = this;
    that.setData({
      unitType: e.detail.value
    });
  },
  //录入数量
  onNumber: function(e) {
    var that = this;
    that.setData({ num: e.detail.value.replace(/[^\d.]/g, '') });
    that.setData({ num: e.detail.value.replace(/\.{2,}/g, '.') });
    that.setData({
      num: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    });
    that.setData({
      num: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    });
  },

  //录入重量
  onNumberWeight: function(e) {
    var that = this;
    that.setData({ weight: e.detail.value.replace(/[^\d.]/g, '') });
    that.setData({ weight: e.detail.value.replace(/\.{2,}/g, '.') });
    that.setData({
      weight: e.detail.value
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
    });
    that.setData({
      weight: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    });
  },

  bindFormSubmit: function() {
    var that = this;
    var categoryIdOne = that.data.categoryIdOne;
    var categoryIdTwo = that.data.categoryIdTwo;
    var num = that.data.num;
    var weight = that.data.weight;

    if (!that.data.categoryStatus) {
      wx.showToast({
        title: '回收类别必选',
        icon: 'none'
      });
      return false;
    } else if (!num) {
      wx.showToast({
        title: '数量必填',
        icon: 'none'
      });
      return false;
    } else if (!weight) {
      wx.showToast({
        title: '单位重量必填',
        icon: 'none'
      });
      return false;
    } else {
      that.setData({ buthidden: true });
      wx.request({
        method: 'POST',
        url: 'https://www.zaishengwang.net/dabaozhan/wx_app/addInventory', //新增库存盘点
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: wx.getStorageSync('userId'),
          categoryId: that.data.twoid,
          parentId: that.data.oneid,
          categoryName: that.data.twoname,
          num: that.data.num,
          unitWeight: that.data.weight,
          countType: that.data.unitType,
          totalWeight: that.data.weight * that.data.num
        },
        success: function(res) {
          wx.navigateBack({
            delta: 1
          });
        },
        fail: function(error) {
          that.setData({ buthiddenNext: true });
        }
      });
    }
  },
  noScroll: function() {
    //微信小程序有bind和catch两种事件绑定方法，bind事件绑定不会阻止冒泡事件向上冒泡，catch事件绑定可以阻止冒泡事件向上冒泡。我们使用catch绑定手指触摸事件，然后他的父节点就不会收到事件冒泡，这样就实现了禁止页面上下滑动
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},
  //查询当前用户所有分类
  queryall() {
    var that = this;
    wx.request({
      method: 'POST',
      url: 'https://www.zaishengwang.net/dabaozhan/wx_app/getListCategory',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: wx.getStorageSync('userId')
      },
      success: function(res) {
        var arr1 = [];
        var arr2 = [];
        var multiArray = [];
        var parentsid = [];
        for (var i = 0; i < res.data.result.length; i++) {
          var obj = {};
          obj.name = res.data.result[i].name;
          obj.parid = res.data.result[i].categoryId;
          obj.array = [];
          for (var k = 0; k < res.data.result[i].categoryList.length; k++) {
            obj.array.push({
              regid: res.data.result[i].categoryId,
              parid: res.data.result[i].categoryList[k].categoryId,
              regname: res.data.result[i].categoryList[k].name
            });
          }
          parentsid.push(obj.parid);
          multiArray.push(obj);
          arr1.push(res.data.result[i].name);
        }
        that.setData({ objectMultiArray: multiArray });
        for (var j = 0; j < res.data.result[0].categoryList.length; j++) {
          arr2.push(res.data.result[0].categoryList[j].name);
        }
        var arr = [arr1, arr2];
        that.setData({ multiArray: arr });
      },
      fail: function(error) {
        that.setData({ buthiddenNext: true });
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.queryall();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
